package ar.com.mooral.activities;

import java.util.Calendar;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import ar.com.mooral.crohn.CrohnAlarmReceiver;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.data.Alarms;
import ar.com.mooral.data.Depositions;
import ar.com.mooral.data.FoodList;
import ar.com.mooral.data.Medications;
import ar.com.mooral.data.Result;
import ar.com.mooral.data.SexualActivityIncidence;
import ar.com.mooral.pojo.AlarmPojo;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.FoodPojo;
import ar.com.mooral.pojo.MedicationPojo;


public class MedicationAdd extends CrohnActivity {
	
	private Medications medications;

	private Context context;
	
	private MedicationPojo pojo;
	
	
	protected void onStart() {
		super.onStart();

		if (CrohnContext.isPojoLoaded()) {
			pojo = (MedicationPojo)CrohnContext.getPojo();
		
			String doses = pojo.getDosesQuantity() + " " + pojo.getDosesUnits();
			doses = doses.replaceAll("null", "");
			if (!doses.trim().equals("")) {
				((Button) findViewById(R.id.btnMedicationAddDoses)).setText(doses);
			}
	
			String frequency = pojo.getFrequencyQuantity() + " " + pojo.getFrequencyEach() + " " + pojo.getFrequencyTime();
			frequency = frequency.replaceAll("null", "");
			if (!frequency.trim().equals("")) {
				((Button) findViewById(R.id.btnMedicationAddFrequency)).setText(frequency);
			}
		
		}

	}


	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medication_add);
        
        context = this;
        
        medications = new Medications(this);
        
		if (!CrohnContext.isPojoLoaded()) {
			MedicationPojo newPojo = new MedicationPojo();
			CrohnContext.setPojo(newPojo);
		}
		pojo = (MedicationPojo)CrohnContext.getPojo();
		EditText txtMedicationAddName = (EditText)findViewById(R.id.txtMedicationAddName);
		txtMedicationAddName.setText(pojo.getName());
		
		if (pojo.getComun() != null && pojo.getComun().equals("1")) {
			txtMedicationAddName.setEnabled(false);	
		} else {
			txtMedicationAddName.setEnabled(true);
		}
		
		
		// RETORNO
        Button btnMedicationAddCancel = (Button) findViewById(R.id.btnMedicationAddCancel);
        btnMedicationAddCancel.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	finish();
		    }
		});		
		
        
		// ADD FREQUENCY
        Button btnMedicationAddFrequency = (Button) findViewById(R.id.btnMedicationAddFrequency);
        btnMedicationAddFrequency.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(	MedicationAddFrequency.class.getPackage().getName(), 
						MedicationAddFrequency.class.getName()));
				startActivity(intent);
		    }
		});		

		// ADD DOSES
        Button btnMedicationAddDoses = (Button) findViewById(R.id.btnMedicationAddDoses);
        btnMedicationAddDoses.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(	MedicationAddDoses.class.getPackage().getName(), 
						MedicationAddDoses.class.getName()));
				startActivity(intent);
		    }
		});		

		// SAVE
        Button lblMedicationAddSave = (Button) findViewById(R.id.btnMedicationAddSave);
        lblMedicationAddSave.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {

		    	pojo.setName(((TextView)findViewById(R.id.txtMedicationAddName)).getText().toString());
		    	
		    	Result r = medications.save(pojo);
		        if (r.hasError()) {
		        	//showMessage(r);
		        } else {
			    	finish();
		        }
		    }
		});		

        

        // SET ALARM
        Button btnMedicationAddSetAlarm = (Button) findViewById(R.id.btnMedicationAddSetAlarm);
        btnMedicationAddSetAlarm.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	
		    	Alarms alarms = new Alarms(context);
		    	Hashtable c = new Hashtable();
		    	c.put("medicacion_id", pojo.getId());
		    	Vector r = alarms.findAll(c);
		    	
		    	AlarmPojo newPojo;
		    	if (r.size() == 1) {
		    		newPojo = (AlarmPojo)r.get(0);
		    		newPojo.parseHours();
		    	} else {
					newPojo = new AlarmPojo();
					newPojo.setDate(CrohnContext.getDateTimeNow("dd/MM/yyyy"));
			    	newPojo.setMedicationId(pojo.getId());
			    	newPojo.setObservations(pojo.getName());
		    	}
				CrohnContext.setAlarmPojo(newPojo);
		    	
		    	
		    	
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(Alarma.class.getPackage().getName(), 
						Alarma.class.getName()));
				startActivity(intent);
		    	

		        //AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

		    	/*
		        // get a Calendar object with current time
		        Calendar cal = Calendar.getInstance();
		        cal.add(Calendar.MINUTE, 1);
		        
		        Intent alarmIntent = new Intent(MedicationAdd.this, CrohnAlarmReceiver.class);
		        alarmIntent.putExtra("alarm_message", "Mi alarma Unica");
		        // In reality, you would want to have a static variable for the request code instead of 192837
		        PendingIntent sender = PendingIntent.getBroadcast(context, 192837, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		        // Get the AlarmManager service
		        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
		        */
		        

		    	/* OK
		        Intent alarmIntentRepetitive = new Intent(MedicationAdd.this, CrohnAlarmReceiver.class);
		        alarmIntentRepetitive.putExtra("alarm_message", "Mi alarma Repetitiva");
		        PendingIntent senderRepetitive = PendingIntent.getBroadcast(context, 192837, alarmIntentRepetitive, PendingIntent.FLAG_UPDATE_CURRENT);

		        long firstTime = SystemClock.elapsedRealtime();
		        //Si descomentas esta linea ya se activa la alerta.
		        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, senderRepetitive);
		        */
		    }
		});
        
        
    }
}
